package com.avecsys.quotemanager.mbeans.menu;

import java.util.ArrayList;
import java.util.List;

import javax.faces.context.FacesContext;
import javax.faces.event.ActionEvent;

import org.icefaces.ace.model.table.RowStateMap;

import com.avecsys.quotemanager.constants.BusinessCodes;
import com.avecsys.quotemanager.mbeans.core.BaseMBean;
import com.avecsys.quotemanager.pojos.UserMasterPojo;
import com.avecsys.quotemanager.pojos.menu.RootMenuEntity;
import com.avecsys.quotemanager.pojos.menu.SubMenuEntity;
import com.avecsys.quotemanager.service.menu.MenuManagerService;
import com.avecsys.quotemanager.utils.MessageBoardUtil;
import com.avecsys.quotemanager.utils.SystemConfig;
import com.icesoft.faces.component.menubar.MenuItem;
import com.icesoft.faces.context.effects.JavascriptContext;

public class MenuAccordionMBean extends BaseMBean
{

	private MenuManagerService menuManagerService;

	private List<RootMenuEntity> rootMenuEntityList;

	private List<AccordionMenuItem> accordionMenuItemList = new ArrayList<AccordionMenuItem>();

	/**
	 * @return the accordionMenuItemList
	 */
	public List<AccordionMenuItem> getAccordionMenuItemList()
	{
		return accordionMenuItemList;
	}

	/**
	 * @param accordionMenuItemList the accordionMenuItemList to set
	 */
	public void setAccordionMenuItemList(List<AccordionMenuItem> accordionMenuItemList)
	{
		this.accordionMenuItemList = accordionMenuItemList;
	}

	/**
	 * @return the menuManagerService
	 */
	public MenuManagerService getMenuManagerService()
	{
		return menuManagerService;
	}

	/**
	 * @param menuManagerService the menuManagerService to set
	 */
	public void setMenuManagerService(MenuManagerService menuManagerService)
	{
		this.menuManagerService = menuManagerService;
		initilizeMenu();
	}

	/**
	 * @return the rootMenuEntityList
	 */
	public List<RootMenuEntity> getRootMenuEntityList()
	{
		return rootMenuEntityList;
	}

	/**
	 * @param rootMenuEntityList the rootMenuEntityList to set
	 */
	public void setRootMenuEntityList(List<RootMenuEntity> rootMenuEntityList)
	{
		this.rootMenuEntityList = rootMenuEntityList;
	}

	private RowStateMap rowStateMap = new RowStateMap();

	private List<AccordionMenuItem> menuItems = new ArrayList<AccordionMenuItem>();

	public MenuAccordionMBean()
	{
		super();
	}

	/**
	 * @return the menuItems
	 */
	public List<AccordionMenuItem> getMenuItems()
	{
		return menuItems;
	}

	/**
	 * @param menuItems the menuItems to set
	 */
	public void setMenuItems(List<AccordionMenuItem> menuItems)
	{
		this.menuItems = menuItems;
	}

	/**
	 * @return the rowStateMap
	 */
	public RowStateMap getRowStateMap()
	{
		return rowStateMap;
	}

	/**
	 * @param rowStateMap the rowStateMap to set
	 */
	public void setRowStateMap(RowStateMap rowStateMap)
	{
		this.rowStateMap = rowStateMap;
	}

	public void actionSelectedMenuItem(ActionEvent actionEvent)
	{
		MessageBoardUtil.resetMessageBoard();
		MenuItem menuItem = (MenuItem) actionEvent.getSource();
		menuItem.setStyleClass("samClass");
		JavascriptContext.addJavascriptCall(FacesContext.getCurrentInstance(), "updateFrame('"
				+ menuItem.getLink()
					+ "');");
	}

	public void initilizeMenu()
	{
		List<RootMenuEntity> rootMenuEntityList = menuManagerService.getAllMenus();
		buildMenuBar(rootMenuEntityList);
	}

	/**
	 * Method to build all the menu system from the database
	 * 
	 * @param rootMenuEntityList
	 */
	public void buildMenuBar(List<RootMenuEntity> rootMenuEntityList)
	{
		String contextPath = SystemConfig.getProperty("pagecontextpath");
		getAccordionMenuItemList().clear();
		UserMasterPojo userMasterPojo = getSignedInUser();
		if (userMasterPojo != null)
		{
			String userType = userMasterPojo.getUserType();
			int index = 0;
			for (RootMenuEntity rootMenuEntity : rootMenuEntityList)
			{
				AccordionMenuItem accordionMenuItem = new AccordionMenuItem();
				accordionMenuItem.setRootMenuEntity(rootMenuEntity);
				accordionMenuItem.setLocaleMenuName(getMenuName(rootMenuEntity.getRootMenuName()));
				List<MenuItem> menuItemList = new ArrayList<MenuItem>();
				List<SubMenuEntity> subMenusList = rootMenuEntity.getSubMenuEntityList();
				for (SubMenuEntity subMenuEntity : subMenusList)
				{
					if (subMenuEntity != null)
					{
						index++;
						MenuItem menuItem = new MenuItem();
						menuItem.setId("menuItem" + index);
						menuItem.setLink(subMenuEntity.getLinkPath());
						menuItem.setValue(getMenuName(subMenuEntity.getSubMenuName()));
						menuItem.setLink(contextPath + subMenuEntity.getLinkPath());
						menuItem.setTarget("contentFrame");
						if (subMenuEntity.getIcon() != null
								&& subMenuEntity.getIcon().trim().length() > 0)
						{
							menuItem.setIcon(subMenuEntity.getIcon());
						}
						menuItemList.add(menuItem);
					}
				}

				if (BusinessCodes.USER_TYPE_SYSTEM_ADMIN.equals(userType))
				{
					// Menus I can see as system Admin
					if (BusinessCodes.MENU_TYPE_SYSTEM_ADMIN.equals(rootMenuEntity.getRootMenuRole()))
					{
						accordionMenuItem.setMenuItemList(menuItemList);
						getAccordionMenuItemList().add(accordionMenuItem);
					}
				}
				else if (BusinessCodes.USER_TYPE_SUPPLIER.equals(userType))
				{
					// Menus I can see as Supplier
					if (BusinessCodes.MENU_TYPE_SUPPLIER.equals(rootMenuEntity.getRootMenuRole()))
					{
						accordionMenuItem.setMenuItemList(menuItemList);
						getAccordionMenuItemList().add(accordionMenuItem);
					}
				}
				else if (BusinessCodes.USER_TYPE_BUYER.equals(userType))
				{
					// Menus I can see as Buyer
					if (BusinessCodes.MENU_TYPE_BUYER.equals(rootMenuEntity.getRootMenuRole()))
					{
						accordionMenuItem.setMenuItemList(menuItemList);
						getAccordionMenuItemList().add(accordionMenuItem);
					}
				}
			}
		}
		else
		{
			actionLogout(null);
		}
	}
}
